package com.mall.user.controller;


import com.mall.common.util.JwtToken;
import com.mall.common.util.MD5;
import com.mall.dependency.util.IPUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.mall.user.model.UserInfo;
import com.mall.common.util.RespResult;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.mall.user.service.IUserInfoService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;


/**
 * <p>
 * 用户表 控制器
 * </p>
 *
 * @author Darren
 * @since 2022-08-29
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@Api(tags = "REST - 用户表")
@RestController
@RequestMapping("/user")
public class UserInfoController {
    private final IUserInfoService service;

    @PostMapping("/addUserInfo")
    @ApiOperation(value = "新增")
    public RespResult addUserInfo(@RequestBody UserInfo userinfo){
        boolean flag = service.addUserInfo(userinfo);
        return RespResult.ok(flag ? "创建成功" : "创建失败");
    }

    @DeleteMapping("/delete/{username}")
    @ApiOperation(value = "根据id删除")
    public RespResult deleteById(@PathVariable("username") String username){
        boolean flag = service.deleteById(username);
        return RespResult.ok(flag ? "删除成功" : "删除失败");
    }

    @PutMapping("/updateUserInfo")
    @ApiOperation(value = "更新")
    public RespResult updateUserInfo(@RequestBody UserInfo userinfo){
        boolean flag = service.updateUserInfo(userinfo);
        return RespResult.ok(flag ? "更新成功" : "更新失败");
    }

    @GetMapping("/{username}")
    @ApiOperation(value = "根据id查询")
    public RespResult getById(@PathVariable("username") String username){
        return RespResult.ok(service.getById(username));
    }

    @PostMapping("/selectList")
    @ApiOperation(value = "根据条件查询全部数据")
    public RespResult selectList(UserInfo userinfo){
        return RespResult.ok(service.selectList(userinfo));
    }

    @PostMapping("/selectPage")
    @ApiOperation(value = "分页查询")
    public RespResult selectPage(Page<UserInfo> page, UserInfo userinfo){
        return RespResult.ok(service.selectPage(page, userinfo));
    }

    @PostMapping("/login")
    @ApiOperation(value = "登录")
    public RespResult<String> login(@RequestParam("username") String username,
                                    @RequestParam("password") String password,
                                    HttpServletRequest request) throws Exception {
        // 查询用户
        UserInfo userInfo = service.getById(username);
        // 匹配
        if (userInfo != null && password.equals(userInfo.getPassword())) {
            // 封装令牌
            Map<String, Object> map = new HashMap<>(4);
            map.put("username", userInfo.getUsername());
            map.put("name", userInfo.getName());
            map.put("roles", userInfo.getRoles());
            // 获取ip
            String ip = IPUtils.getIp(request);
            map.put("ip", MD5.md5(ip));
            // 创建token
            String token = JwtToken.createToken(map);
            return RespResult.ok(token);
        }
        return RespResult.error("账号或密码错误~");
    }
}